//
//  EMCityChooseViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 15/1/7.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMCityChooseViewController.h"
#import "EMSearchBar.h"
@interface EMCityChooseViewController ()
{
    NSArray *cityArray;
    NSArray *titleArray;
}
@end

@implementation EMCityChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGB(242, 242, 242);
    cityArray = @[@"南京", @"无锡", @"常州", @"武进", @"戚墅堰", @"镇江", @"京口区", @"润州区", @"丹徒新区", @"丹阳市", @"句容市", @"扬中市", @"大港新区"];
    titleArray = @[@"常州", @"钟楼", @"武进", @"新北", @"栖霞", @"仙林", @"惠山", @"河埒口", @"万达", @"江宁", @"江山", @"铜陵", @"呼伦贝尔", @"伊利牧场", @"蒙牛草原", @"稻草人"];

    UILabel *cityButton = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 140, 24)];
    cityButton.text = @"当前城市：南京郊区";
    cityButton.textColor = RGB(101, 118, 32);
    cityButton.font = [UIFont fontWithName:HEITIMEDIUM size:14];
    [self.view addSubview:cityButton];
    EMSearchBar *search = [[EMSearchBar alloc] initWithFrame:CGRectMake(160, 30, 140, 24)];
    search.placeholderText = @"请输入城市名或字母";
    [self.view addSubview:search];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, 320, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineView];
    UIView *headerView = [[UIView alloc] init];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 25)];
    titleLabel.text = @"热门城市:";
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
    [headerView addSubview:titleLabel];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 40, 290, 115) collectionViewLayout:layout];
    [contentCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cityHeadCell"];
    contentCollectionView.delegate = self;
    contentCollectionView.dataSource = self;
    contentCollectionView.backgroundColor = RGB(242, 242, 242);
    [headerView addSubview:contentCollectionView];
    headerView.frame = CGRectMake(0, 60, 320, 115);
    [self.view addSubview:headerView];
   
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 185, 320, [UIScreen mainScreen].bounds.size.height - 185) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = RGB(242, 242, 242);
    [self.view addSubview:table];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return cityArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cityCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
    }
    
    cell.textLabel.text = cityArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return titleArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cityHeadCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell sizeToFit];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:cell.bounds];
    titleLabel.text = titleArray[indexPath.row];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.layer.borderWidth = 1;
    titleLabel.layer.borderColor = RGB(112, 179, 81).CGColor;
    titleLabel.layer.cornerRadius = 5;
    [cell addSubview:titleLabel];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets top = {0, 0, 0, 0};
    return top;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *titleC = titleArray[indexPath.row];
    NSDictionary *attDic = @{NSFontAttributeName: [UIFont systemFontOfSize:12]};
    CGSize fontSize = [titleC sizeWithAttributes:attDic];
    return CGSizeMake(fontSize.width + 10, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
}
@end
